package com.magima.utils.audio;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @package com.yst_address_book
 * @author Allen
 * @email jaylong1302@163.com
 * @data 2013-3-5 6:36:34
 * @description The class is for...
 */
public class Pinyin4j {
	private static HashMap<String, String> mLastNamePinyin;
	
	private static HashMap<String, String> getLastNamePinyin(){
		if ( mLastNamePinyin == null ) {
			mLastNamePinyin = new HashMap<String, String>();
			mLastNamePinyin.put("沈", "shen3");
			mLastNamePinyin.put("单", "shan4");
			mLastNamePinyin.put("仇", "qiu2");
			mLastNamePinyin.put("区", "ou1");
			mLastNamePinyin.put("召", "shao4");
			mLastNamePinyin.put("任", "ren2");
			mLastNamePinyin.put("华", "hua4");
			mLastNamePinyin.put("朴", "piao2");
			mLastNamePinyin.put("折", "she2");
			mLastNamePinyin.put("黑", "he4");
			mLastNamePinyin.put("解", "xie4");
			mLastNamePinyin.put("繁", "po2");
			mLastNamePinyin.put("纪", "ji3");
			mLastNamePinyin.put("查", "zha1");
			mLastNamePinyin.put("秘", "bi4");
			mLastNamePinyin.put("褚", "chu3");
			mLastNamePinyin.put("弗", "fei4");
			mLastNamePinyin.put("藉", "ji2");
			mLastNamePinyin.put("适", "kuo4");
			mLastNamePinyin.put("句", "gou1");
			mLastNamePinyin.put("阚", "kan");
			mLastNamePinyin.put("乜", "nie4");
			mLastNamePinyin.put("眭", "sui2");
			mLastNamePinyin.put("洗", "xian3");
			mLastNamePinyin.put("燕", "yan1");
			mLastNamePinyin.put("员", "yun4");
			mLastNamePinyin.put("祭", "zhai4");
			mLastNamePinyin.put("厘", "xi1");
			mLastNamePinyin.put("宿", "su4");
			mLastNamePinyin.put("缪", "miao4");
			mLastNamePinyin.put("叶", "ye4");
			mLastNamePinyin.put("强", "qiang2");
		}
		return mLastNamePinyin;
	}
	/**
	 * �ַ���ת���ַ�(���ŷָ�)
	 * 
	 * @author wyh
	 * @param stringSet
	 * @return
	 */
	public static String makeStringByStringSet(Set<String> stringSet) {
		StringBuilder str = new StringBuilder();
		for (String s : stringSet) {
			str.append(s);
		}
		return str.toString().toLowerCase();
	}

	/**
	 * ��ȡƴ������
	 * 
	 * @author wyh
	 * @param src
	 * @return Set<String>
	 */
	private static HanyuPinyinOutputFormat hanYuPinOutputFormat = null;
	
	public static Set<String> getPinyin(String src, boolean isName) {
		if (src != null && !src.trim().equalsIgnoreCase("")) {
			char[] srcChar;
			srcChar = src.toCharArray();
			if ( hanYuPinOutputFormat == null ) {
				hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
	
				hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
				hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);
				hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
			}

			String[][] temp = new String[src.length()][];
			for (int i = 0; i < srcChar.length; i++) {
				char c = srcChar[i];
				String strChar = String.valueOf(c);
				if ( i==0 && isName && getLastNamePinyin().get(strChar) != null ) {
					temp[i] = new String[] { getLastNamePinyin().get(strChar) };
				} else if (strChar.matches("[\\u4E00-\\u9FA5]+")) {
					try {
						temp[i] = PinyinHelper.toHanyuPinyinStringArray(
								srcChar[i], hanYuPinOutputFormat);
					} catch (BadHanyuPinyinOutputFormatCombination e) {
						e.printStackTrace();
					}
				} else if (((int) c >= 65 && (int) c <= 90)
						|| ((int) c >= 97 && (int) c <= 122)
						|| ((int) c >= 48 && (int) c <= 57)){
					temp[i] = new String[] { String.valueOf(srcChar[i]) };
				} else {
					temp[i] = new String[] { "" };
				}
			}
			String[] pingyinArray = Exchange(temp);
			Set<String> pinyinSet = new HashSet<String>();
			for (int i = 0; i < pingyinArray.length; i++) {
				pinyinSet.add(pingyinArray[i]);
			}
			return pinyinSet;
		}
		return null;
	}

	/**
	 * �ݹ�
	 * 
	 * @author wyh
	 * @param strJaggedArray
	 * @return
	 */
	public static String[] Exchange(String[][] strJaggedArray) {
		String[][] temp = DoExchange(strJaggedArray);
		return temp[0];
	}

	/**
	 * �ݹ�
	 * 
	 * @author wyh
	 * @param strJaggedArray
	 * @return
	 */
	private static String[][] DoExchange(String[][] strJaggedArray) {
		int len = strJaggedArray.length;
		if (len >= 2) {
			int len1 = strJaggedArray[0].length;
			int len2 = strJaggedArray[1].length;
			int newlen = len1 * len2;
			String[] temp = new String[newlen];
			int Index = 0;
			for (int i = 0; i < len1; i++) {
				for (int j = 0; j < len2; j++) {
					temp[Index] = strJaggedArray[0][i] + strJaggedArray[1][j];
					Index++;
				}
			}
			String[][] newArray = new String[len - 1][];
			for (int i = 2; i < len; i++) {
				newArray[i - 1] = strJaggedArray[i];
			}
			newArray[0] = temp;
			return DoExchange(newArray);
		} else {
			return strJaggedArray;
		}
	}

	/* ��ȡ����ƴ�� */
	public static String getHanyuPinyin(String word) {
		Set<String> set = getPinyin(word, false);
		if ( set == null )
			return "";
		return makeStringByStringSet(set);
	}

	public static String getChineseNameHanyuPinyin(String word) {
		Set<String> set = getPinyin(word, true);
		if ( set == null )
			return "";
		return makeStringByStringSet(set);
	}
}
